import jade.util.Coord;
import jade.util.Dice;

import java.util.HashMap;
import java.util.Map;

class Dungeon {
	PrisonEscape parent;
	Dice random;
	private int depth;
	private Map<Integer, Level> levels;
	private int levelWidth;
	private int levelHeight;

	Dungeon(PrisonEscape parent, Dice random, int levelWidth, int levelHeight) {
		this.parent = parent;
		depth = 1;
		levels = new HashMap<Integer, Level>();
		this.levelWidth = levelWidth;
		this.levelHeight = levelHeight;
		this.random = random;
	}

	boolean ascend() {
		Creature player = getLevel().getPlayer();
		if (getLevel().getTile(player.getX(), player.getY()).getLook().getCh() != '<') {
			player.appendMessage("No stairs here");
			return false;
		} else {
			changeLevel(depth - 1);
			return true;
		}
	}

	private void changeLevel(int newDepth) {
		boolean goingDown = newDepth > depth;
		Creature player = getLevel().getPlayer();
		getLevel().removeActor(player);
		depth = newDepth;
		parent.level = getLevel();
		Coord pos = goingDown ? getLevel().getUpStairs() : getLevel().getDownStairs();
		getLevel().addActor(player, pos);
	}

	boolean descend() {
		Creature player = getLevel().getPlayer();
		if (getLevel().getTile(player.getX(), player.getY()).getLook().getCh() != '>') {
			player.appendMessage("No stairs here");
			return false;
		} else {
			changeLevel(depth + 1);
			return true;
		}
	}

	int getDepth() {
		return depth;
	}

	Level getLevel() {
		Level level = levels.get(depth);
		if (level == null) {
			level = new Level(parent, this, depth, levelWidth, levelHeight);
			levels.put(depth, level);
		}
		return level;
	}
}
